<template>
  <div class="vehicle-container">
    <el-row class="vehicle-header">
      <el-card>
        <el-form label-width="auto" :model="form">
          <el-row type="flex" justify="space-between">
            <el-form-item label="车型编号">
              <el-col :span="8">
                <el-input
                  v-model="form.id"
                  clearable
                  class="inputStyle"
                  placeholder="请输入车型编号"
                />
              </el-col>
            </el-form-item>
            <el-form-item label="应载重量">
              <el-col :span="8">
                <el-select
                  v-model="form.allowableLoad"
                  clearable
                  class="inputStyle"
                  placeholder="请输入应载重量"
                >
                  <el-option
                    v-for="item in vehicleLoad"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="应载体积">
              <el-col :span="8">
                <el-select
                  v-model="form.allowableVolume"
                  clearable
                  class="inputStyle"
                  placeholder="请输入应载体积"
                >
                  <el-option
                    v-for="item in vehicleVolume"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-col>
            </el-form-item>
          </el-row>
          <el-row type="flex">
            <el-form-item style="margin: 0" label="车辆类型">
              <el-input
                v-model="form.name"
                clearable
                placeholder="请输入车辆类型"
                class="inputStyle"
              />
            </el-form-item>
            <el-form-item style="margin: 0">
              <el-button
                class="buttonStyle"
                type="primary"
                size="small"
                @click="searchClick"
              >查询</el-button>
              <el-button
                class="buttonStyle"
                size="small"
                @click="resetClick"
              >重置</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-card>
    </el-row>
    <el-row class="vehicle-footer">
      <el-card class="footer-card">
        <el-row>
          <el-button
            size="small"
            type="primary"
            class="footer-button"
            @click="addVehicle"
          >新增车型</el-button>
        </el-row>
        <el-row>
          <el-table :data="tableData" stripe style="width: 100%" border>
            <el-table-column prop="date" label="序号" width="50" align="center">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="id"
              label="车型编号"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="name"
              label="车型类型"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="num"
              label="车型数量"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="allowableLoad"
              label="应载重量"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="allowableVolume"
              label="应载体积"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="measureLong"
              label="长（米）"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="measureWidth"
              label="宽（米）"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="measureHigh"
              label="高（米）"
              width="128.6"
            />
            <el-table-column
              align="center"
              prop="address"
              label="操作"
              fixed="right"
            >
              <template #default="{ row }">
                <el-row type="flex" justify="center" align="middle">
                  <el-button
                    size="small"
                    type="text"
                    @click="editClick(row)"
                  >编辑</el-button>
                  <div class="centerLine" />
                  <el-button
                    size="small"
                    type="text"
                    style="color: red"
                    @click="delClick(row)"
                  >删除</el-button>
                </el-row>
              </template>
            </el-table-column>
            <template slot="empty">
              <el-empty :image-size="150" description="暂无数据" />
            </template>
          </el-table>
        </el-row>
        <el-row
          type="flex"
          justify="center"
          style="margin-top: 10px; height: 40px"
          align="middle"
        >
          <el-pagination
            :current-page="form.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-count="pages"
            :page-size="form.pageSize"
            :pager-count="7"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="gurrentChange"
          />
        </el-row>
      </el-card>
    </el-row>
    <!-- 新增车型 -->
    <el-dialog
      class="addDialog"
      :title="vehicleID ? '编辑车型' : '新增车型'"
      :visible.sync="dialogFormVisible"
      width="35%"
      @close="addDialogCancel"
    >
      <el-form
        ref="addDialogForm"
        :model="addVehicleForm"
        :rules="rules"
        label-width="auto"
      >
        <el-form-item label="车辆类型" prop="name">
          <el-input
            v-model="addVehicleForm.name"
            clearable
            placeholder="请输入车辆类型"
            class="dialogInputStyle"
          />
        </el-form-item>
        <el-form-item label="应载重量" prop="allowableLoad">
          <el-input
            v-model="addVehicleForm.allowableLoad"
            class="dialogInputStyle"
            placeholder="请输入应载重量"
            autocomplete="off"
            clearable
          />
          <span class="units">吨</span>
        </el-form-item>
        <el-form-item label="应载体积" prop="allowableVolume">
          <el-input
            v-model="addVehicleForm.allowableVolume"
            class="dialogInputStyle"
            placeholder="请输入应载体积"
            autocomplete="off"
            clearable
          />
          <span class="units">立方</span>
        </el-form-item>
        <el-form-item label="长" prop="measureLong">
          <el-input
            v-model="addVehicleForm.measureLong"
            class="dialogInputStyle"
            placeholder="请输入长"
            autocomplete="off"
            clearable
          />
          <span class="units">米</span>
        </el-form-item>
        <el-form-item label="宽" prop="measureWidth">
          <el-input
            v-model="addVehicleForm.measureWidth"
            class="dialogInputStyle"
            placeholder="请输入宽"
            autocomplete="off"
            clearable
          />
          <span class="units">米</span>
        </el-form-item>
        <el-form-item label="高" prop="measureHigh">
          <el-input
            v-model="addVehicleForm.measureHigh"
            class="dialogInputStyle"
            placeholder="请输入高"
            autocomplete="off"
            clearable
          />
          <span class="units">米</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          class="buttonStyle"
          type="primary"
          @click="addDialogConfirm"
        >确 定</el-button>
        <el-button
          size="small"
          class="buttonStyle"
          @click="addDialogCancel"
        >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addVehicleTypeAPI,
  delVehicleTypeAPI,
  getVehicleTypeAPI,
  getVehicleTypeDetailAPI,
  updateVehicleTypeAPI
} from '@/api/vehicle'
import { vehicleLoad, vehicleVolume } from '@/canstant/vehicle'
export default {
  data() {
    return {
      // 车型弹框开关
      dialogFormVisible: false,
      // 车辆载重
      vehicleLoad,
      // 车辆体积
      vehicleVolume,
      // 表单数据
      form: {
        page: 1,
        pageSize: 10,
        allowableLoad: '',
        allowableVolume: '',
        id: '',
        name: ''
      },
      // 新增车型表单数据
      addVehicleForm: {
        allowableLoad: '',
        allowableVolume: '',
        measureHigh: '0.0',
        measureLong: '0.0',
        measureWidth: '0.0',
        name: ''
      },
      // 新增表单验证
      rules: {
        name: [{ required: true, message: '请输入车辆类型', trigger: 'blur' }],
        allowableLoad: [
          { required: true, message: '请输入应载重量', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '请输入应载体积', trigger: 'blur' }
        ],
        measureLong: [{ required: true, message: '请输入长', trigger: 'blur' }],
        measureWidth: [
          { required: true, message: '请输入宽', trigger: 'blur' }
        ],
        measureHigh: [{ required: true, message: '请输入高', trigger: 'blur' }]
      },
      // 表格数据
      tableData: [],
      // 总页数
      total: 0,
      // 总页数
      pages: 1,
      // 防抖
      timer: null,
      // 车型id
      vehicleID: ''
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        setTimeout(() => {
          this.vehicleID = null
        }, 300)
      }
    }
  },
  created() {
    this.tableListData()
  },
  methods: {
    // 获取表格数据
    async tableListData() {
      const { data } = await getVehicleTypeAPI(this.form)
      const itemData = data.data
      this.pages = +itemData.pages
      this.total = +itemData.counts
      this.tableData = itemData.items
    },
    // 搜索
    searchClick() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.tableListData()
      }, 500)
    },
    // 重置
    resetClick() {
      this.form.allowableLoad = null
      this.form.allowableVolume = null
      this.form.id = null
      this.form.name = null
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.tableListData()
      }, 500)
    },
    // 新增车型
    addVehicle() {
      this.dialogFormVisible = true
    },
    // 车型弹框-确定
    async addDialogConfirm() {
      await this.$refs.addDialogForm.validate()
      if (!this.vehicleID) {
        await addVehicleTypeAPI(this.addVehicleForm)
      } else {
        await updateVehicleTypeAPI(this.vehicleID, this.addVehicleForm)
      }
      this.$refs.addDialogForm.resetFields()
      this.tableListData()
      this.dialogFormVisible = false
    },
    // 车型弹框-取消
    addDialogCancel() {
      this.$refs.addDialogForm.resetFields()
      this.dialogFormVisible = false
    },
    // 编辑
    async editClick({ id }) {
      this.vehicleID = id
      const { data } = await getVehicleTypeDetailAPI(id)
      this.addVehicleForm = data.data
      this.dialogFormVisible = true
    },
    // 删除
    async delClick({ id }) {
      await delVehicleTypeAPI(id)
      this.tableListData()
    },
    // 当前页最大条数
    sizeChange(pageSize) {
      this.form.pageSize = pageSize
      this.tableListData()
    },
    // 当前页
    gurrentChange(page) {
      this.form.page = page
      this.tableListData()
    }
  }
}
</script>

<style scoped lang="scss">
.vehicle-container {
  padding: 0 20px;
  .vehicle-header {
    margin-bottom: 20px;
    .el-form-item {
      padding: 0 20px;
      .inputStyle {
        width: 300px;
      }
      .buttonStyle {
        width: 80px;
        height: 30px;

        &.buttonStyle:first-child {
          background-color: #189450;
          border-color: #189450;
          margin-right: 10px;

          &:hover {
            opacity: 0.7;
          }
        }
      }
    }
    .header-footer {
      margin: 0;
    }
  }
  .vehicle-footer {
    .footer-button {
      margin-bottom: 20px;
      background-color: #189450;
      border-color: #189450;
    }

    .footer-card {
      padding: 10px;
      padding-bottom: 0;
    }
  }
}
// 公共样式
.addDialog {
  ::v-deep .el-dialog {
    border-radius: 10px;
  }
  .dialogInputStyle {
    position: relative;
    width: 320px;
    height: 30px;
  }
  ::v-deep .el-dialog__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-radius: 10px 10px 0 0;
    background-color: #f3f4f7;
  }
  ::v-deep .el-dialog__title {
    font-size: 15px;
    font-weight: bold;
  }
  ::v-deep .el-dialog__headerbtn {
    position: static;
    background: 0 0;
    border: none;
    outline: 0;
    cursor: pointer;
    font-size: 16px;
  }
  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
    padding: 20px 25px 20px;
    font-size: 13px;
  }
  .buttonStyle {
    width: 80px;
    height: 30px;

    &.buttonStyle:first-child {
      background-color: #189450;
      border-color: #189450;
      margin-right: 10px;

      &:hover {
        opacity: 0.7;
      }
    }
  }
  .units {
    position: absolute;
    right: 25px;
    top: 50%;
    transform: translateY(-50%);
    display: inline-block;
    height: 30px;
    line-height: 30px;
    text-align: right;
    font-size: 13px;
    color: #606266;
  }
}
.centerLine {
  box-sizing: border-box;
  margin: 0 10px;
  width: 1px;
  height: 13px;
  background: #d0d3da;
}
::v-deep .el-form-item__label {
  font-size: 13px;
}

::v-deep .el-table thead tr th {
  font-size: 13px;
  font-weight: bold;
  background: #f8faff !important;
  border: none;
}
::v-deep .el-table tbody tr td {
  font-size: 13px;
  color: #606266;
  border: none;
}
::v-deep .el-input__inner {
  height: 35px;
  font-size: 13px;
}
::v-deep .el-pagination .el-select .el-input .el-input__inner {
  height: 28px;
  font-size: 13px;
}
</style>
